package com.study.publish;

import java.util.ArrayList;
import java.util.List;

import com.study.subscription.ISubscription;

public class Publish extends APublish {

    private List<ISubscription> subscripts;

    public Publish(String name) {
        this.name = name;
        subscripts = new ArrayList<>();
    }

    @Override
    public void add(ISubscription subscript) {
        System.out.println(name + "增加一个订阅者");
        subscripts.add(subscript);

    }

    @Override
    public void delete(ISubscription subscript) {
        for (int i = 0; i < subscripts.size(); i++) {
            if (subscripts.get(i) == subscript) {
                subscripts.remove(i);
                System.out.println(name + "缺少一个订阅者");
            }
        }
    }

    @Override
    public void send(String msg) {
        for (ISubscription iSubscript : subscripts) {
            iSubscript.update(msg);

        }
        System.out.println(name + "消息发送成功");
    }

}
